﻿/***************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@gmail.com)

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using AspNetMembership = System.Web.Security.Membership;
using AspNetRoles = System.Web.Security.Roles;
using BaoJianSoft.Common;
using BaoJianSoft.Membership;
using BaoJianSoft.Membership.Linq;
using BaoJianSoft.Web.Extensions.DynamicPages;
using BaoJianSoft.Web.Extensions.Controls;
using ComponentArt.Web.UI;

namespace BaoJianSoft.Web.Extensions.DynamicPages
{
	/// <summary>
	/// Class of popup selector dialog
	/// </summary>
	public class PopupSelectorDialog : PageWorkshop
	{
		/// <summary>
		/// Get opener callback control id for selected value.
		/// </summary>
		protected string OpenerCallbackControlId { get { return HttpContext.Current.Request["OpenerCallbackControlId"]; } }

		/// <summary>
		/// Get true to append selected value to target control.
		/// </summary>
		protected bool AppendSelectedValue
		{
			get
			{
				if (string.IsNullOrEmpty(HttpContext.Current.Request["AppendSelectedValue"])) return false;

				bool appendSelectedValue;
				if (bool.TryParse(HttpContext.Current.Request["AppendSelectedValue"], out appendSelectedValue))
				{
					return appendSelectedValue;
				}

				return false;
			}
		}

		/// <summary>
		/// Select target value in popup dialog and return it to opener.
		/// </summary>
		/// <param name="returnedValue"></param>
		protected virtual void SelectTargetValue(string returnedValue)
		{
			string jsFormat = null;
			if (this.AppendSelectedValue)
			{
				jsFormat = "var value = window.opener.document.getElementById(\"{0}\").value; \r\n";
				jsFormat += "if (value == null || value.length == 0) window.opener.document.getElementById(\"{0}\").value = '{1}'; \r\n";
				jsFormat += "else window.opener.document.getElementById(\"{0}\").value += \"; \" + '{1}'; \r\n";
			}
			else
			{
				jsFormat = "window.opener.document.getElementById(\"{0}\").value = '{1}'; \r\n";
			}

			jsFormat += "window.close(); \r\n";
			string js = string.Format(jsFormat, this.OpenerCallbackControlId, returnedValue);

			Kit.RegisterScriptBlock("SelectTargetValue", js);
		}
	}
}

